//
//  MainViewController.m
//  ZCJZ
//
//  Created by Mr.han on 2018/10/29.
//  Copyright © 2018 Mr.han. All rights reserved.
//

#import "MainViewController.h"
#import "SYBaseViewController.h"
#import "UIImage+SubImage.h"

#import "SYHomeVC.h"
#import "SYMyCenterVC.h"
#import "ZCOrderListVC.h"

@interface MainViewController ()<UITabBarControllerDelegate>

@end

@implementation MainViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  [self addSubClassTabbarVC];
  self.delegate = self;
}

- (void)dealloc {
  
}

- (void)addSubClassTabbarVC {
  self.tabBar.translucent = NO;
  self.tabBar.backgroundColor = [UIColor whiteColor];
  
  SYHomeVC *homevc = [[SYHomeVC alloc] init];
  ZCOrderListVC *ordervc = [[ZCOrderListVC alloc] init];
  SYMyCenterVC *mycentervc = [[SYMyCenterVC alloc] init];
  
  UINavigationController *nvc0 = [[UINavigationController alloc] initWithRootViewController:homevc];
  UINavigationController *nvc1 = [[UINavigationController alloc] initWithRootViewController:ordervc];
  UINavigationController *nvc2 = [[UINavigationController alloc] initWithRootViewController:mycentervc];
  
  
  homevc.tabBarItem = [self configBarItemWithTitle:@"首页" normalImg:@"tab_sy_n" selectedImage:@"tab_sy_s"];
  ordervc.tabBarItem = [self configBarItemWithTitle:@"订单" normalImg:@"tab_gwc_n" selectedImage:@"tab_gwc_s"];
  mycentervc.tabBarItem = [self configBarItemWithTitle:@"我的" normalImg:@"tab_wd_n" selectedImage:@"tab_wd_s"];
  self.viewControllers = @[nvc0,nvc1,nvc2];
}

- (UITabBarItem *)configBarItemWithTitle:(NSString *)title normalImg:(NSString *)normalImg selectedImage:(NSString *)selectedImage {
  UIImage *img = [UIImage imageNamed:normalImg];
  UIImage *imgs = [UIImage imageNamed:selectedImage];
  img = [img rescaleImageToSize:(CGSize){23,23}];
  imgs = [imgs rescaleImageToSize:(CGSize){23,23}];
  
  imgs = [imgs imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  UITabBarItem *item = [[UITabBarItem alloc]initWithTitle:title image:nil tag:1];
  item.selectedImage = imgs;
  item.image = img;
  self.tabBar.tintColor = [UIColor blackColor];
  [item setTitleTextAttributes:@{NSForegroundColorAttributeName: UIColorFromRGB(0xF9150F)} forState:UIControlStateSelected];
  
  return item;
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
  
  __block NSInteger index = [self.childViewControllers indexOfObject:viewController];
  if (index == 0) {
    return YES;
  }
  return YES;
}

@end
